package com.example.myone.Activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.example.myone.Entity.User;
import com.example.myone.Fragment.MyFragment;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.Mapper.UserMapper;
import com.example.myone.Pager.MyPager;
import com.example.myone.R;
import com.example.myone.Util.ClickUtils;
import com.example.myone.Util.LoginInfoUtil;
import com.example.myone.Util.PasswordEncryptor;

@SuppressLint("NewApi")
public class LoginActivity extends AppCompatActivity {
    private SharedPreferences pref;
    private EditText accountEdit;
    private EditText passwordEdit;
    private CheckBox rememberPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);
        // 初始化UI元素
        accountEdit = findViewById(R.id.account);
        passwordEdit = findViewById(R.id.password);
        rememberPass = findViewById(R.id.remember_pass);
        Button loginButton = findViewById(R.id.login_button);
        TextView toRegister = findViewById(R.id.toRegister);
        // 获取登录文件
        pref = getSharedPreferences("login_account", MODE_PRIVATE);
        boolean isRemember = pref.getBoolean("remember_password", false);
        if (isRemember) { // 如果需要记住密码
            // 将账号和密码都设置到文本框中
            String account = pref.getString("account", "");
            String password = pref.getString("password", "");
            accountEdit.setText(account);
            passwordEdit.setText(password);
            rememberPass.setChecked(true);
        }
        View loginLogo = findViewById(R.id.login_logo);
        ClickUtils.setClickListener(this,loginLogo,null, this::finish);
        // 设置跳转注册点击事件
        ClickUtils.setClickListener(this, toRegister, "前往注册", () -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
            finish();
        });
        // 设置登录按钮的点击事件
        loginButton.setOnClickListener(v -> {
            // 获取用户输入的用户名和密码
            String account = accountEdit.getText().toString();
            String password = passwordEdit.getText().toString();
            if (checkAccount(account, password)) {
                rememberOrNot(account, password); // 保存登录信息
                MyPager.replaceFragment(new MyFragment());
                finish();
            }
        });
    }


    // 检查账号密码
    private boolean checkAccount(String account, String password) {
        User savedUser = UserMapper.getUserByAccount(this, account);
        if (savedUser == null) {
            Toast.makeText(LoginActivity.this, "用户不存在，请先注册", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!PasswordEncryptor.matchPassword(password, savedUser.password)) {
            Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
            return false;
        }
        LoginInfoUtil.save(this,savedUser);
        return true;
    }


    // 保存登录信息
    private void rememberOrNot(String account, String password) {
        // 获取 SharedPreferences 的编辑器对象
        SharedPreferences.Editor editor = pref.edit();
        if (rememberPass.isChecked()) { // 检查复选框是否被选中
            // 如果复选框被选中，保存账号和密码
            editor.putBoolean("remember_password", true);
            editor.putString("account", account);
            editor.putString("password", password);
        } else { // 如果复选框没有被选中，清除之前保存的账号和密码
            editor.clear();
        }
        // 应用编辑器的更改
        editor.apply();
    }


}
